package com.fenshi.presenter.imp;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ImageView;

import com.android.volley.VolleyError;
import com.fenshi.bean.OrderBean;
import com.fenshi.bean.UserBean;
import com.fenshi.interactor.OrderInteractor;
import com.fenshi.interactor.imp.OrderInteractorImp;
import com.fenshi.manager.R;
import com.fenshi.manager.activity.order.OrderDetailActivity;
import com.fenshi.presenter.MainPresenter;
import com.fenshi.presenter.listener.RequestListener;
import com.fenshi.until.CookicUntil;
import com.fenshi.until.SystemUtils;
import com.fenshi.view.MainView;
import com.quickdv.activity.adapter.viewholder.DataViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by lilongfei on 15/10/4.
 */
public class OrderListPresenterImp implements MainPresenter, RequestListener<List<OrderBean>> {

    Context c;
    int currentPage = 1;
    MainView view;
    OrderInteractor interactor;
    List<OrderBean> result;
    String type = "2";

    public OrderListPresenterImp(Context c) {
        this(c, (MainView) c);
    }

    public OrderListPresenterImp(Context c, MainView view) {
        this.c = c;
        this.view = view;
        this.interactor = new OrderInteractorImp(c, this);
    }


    @Override
    public void initDateEvent() {
        UserBean bean = CookicUntil.getUser();
        view.initViewEvent(bean);
    }

    @Override
    public void getOrder(boolean isRefresh) {
        String userId = CookicUntil.getUser().getUserId();
        String parkId = CookicUntil.getUser().getParkId();

        if (isRefresh) currentPage = 1;
        if (userId == null) {
            view.showError("用户未登陆，请先登录");
//            Intent intent = new Intent(c, LoginActivity.class);
//            c.startActivity(intent);
            return;
        } if (parkId ==null){
            view.showError("管理员没有租用点记录");
            return;
        }
        interactor.getOrderList(userId, parkId, type, currentPage);

    }

    @Override
    public void getOrder(String type, boolean isRefresh) {
        String userId = CookicUntil.getUser().getUserId();
        String parkId = CookicUntil.getUser().getParkId();

        if (isRefresh) currentPage = 1;
        if (userId == null) {
            view.showError("用户未登陆，请先登录");
//            Intent intent = new Intent(c, LoginActivity.class);
//            c.startActivity(intent);
            return;
        }if (parkId ==null){
            view.showError("管理员没有租用点记录");
            return;
        }
        view.dismissLoad();
        interactor.getOrderList(userId, parkId, type, currentPage);
    }

    @Override
    public void onImageLoad(DataViewHolder hold, int position) {
        ImageView view = (ImageView) hold.findView(R.id.order_pic);
        if (result.get(position).getCarImg()!=null){
            Picasso.with(c).load(CookicUntil.IMG_URL +result.get(position).getCarImg()).centerCrop().resize(150,120).error(R.drawable.loadfailed).placeholder(R.drawable.loading).into(view);
        }
    }

    @Override
    public void onItemClick(int i) {
        Intent intent = new Intent();
        Bundle bundle = new Bundle();
        if (result.get(i) != null) {
            OrderBean bean = result.get(i);
            bundle.putSerializable(CookicUntil.ORDER, bean);
            intent.putExtras(bundle);
        }
//        if (result.get(i).getStatus().equals(MethodCode.OrderStatus.UNACTIVATION.getValue())) {
//            intent.setClass(c, RentActivity.class);
//        }  else if (result.get(i).getStatus().equals(MethodCode.OrderStatus.RETURN.getValue())) {
//            intent.setClass(c, BackActivity.class);
//        } else if(result.get(i).getStatus().equals(MethodCode.OrderStatus.CHECKCAR.getValue())){
//            intent.setClass(c, BackCarOrderActivity.class);
//        }
        intent.setClass(c, OrderDetailActivity.class);

        c.startActivity(intent);
    }

    @Override
    public void Success(String tag, List<OrderBean> result) {
        view.dismissLoad();
        if (result == null) {
            view.showError("未获取到订单数据");
            return;
        }
        if (result.size() == 0) {
//            view.showError("没有未处理的订单");
            view.setDate(null,true);
            return;
        }
        List<Map<String, Object>> datas = new ArrayList<>();
        for (OrderBean orderBean : result) {
            Map<String, Object> data = SystemUtils.beanToMap(orderBean);
            datas.add(data);
        }
        view.setDate(datas, currentPage == 1);
        view.notifyDateChanged();
        currentPage++;

        if (this.result == null || currentPage==1)
            this.result = result;
        else
            this.result.addAll(result);
    }

    @Override
    public void Error(String tag, String error) {
        view.dismissLoad();
        view.showError(error);
    }

    @Override
    public void Exception(String tag, VolleyError error) {
        view.dismissLoad();
        view.showError(c.getString(R.string.system_error));
    }


}
